package com.example.datastructure;

public class ListNode<T> {
    private int foot;//根节点索引位置
    private int count;//链表长度
    private Node root;//根据节点标识

    //链接点类,内部方法实现，外部使用
    private class Node{
        private T data;//数据域
        private Node next;//指针域

        public Node(T data){
            this.data=data;
        }

        //添加节点
        private void add(T data){
            if (this.next==null){
                this.next=new Node(data);
            }else {
                this.next.add(data);
            }
        }

        //删除节点
        public void remove(Node pre,int index){
            if (ListNode.this.foot++==index){
                pre.next=this.next;
                this.next=null;
                ListNode.this.count--;
                return;
            }else{
                this.next.remove(this,index);
            }
        }
        public void remove(Node pre,T data){
            if (this.data.equals(data)){
                pre.next=this.next;
                this.next=null;
                ListNode.this.count--;
                return;
            }else {
                if (this.next!=null){
                    this.next.remove(this,data);
                }else {
                    return;
                }
            }
        }

        //修改数据
        public  void replace(T oldData,T newData){
            if (this.data.equals(newData)){
                this.data=newData;
            }else {
                this.next.replace(oldData, newData);
            }
        }
        public void replace(int index,T newData){
            if (ListNode.this.foot++==index){
                this.data=newData;
            }else {
                this.next.replace(index, newData);
            }
        }
    }
}
